#include "logger.h"

logger* logger::instance = 0;
boost::mutex logger::instance_mutex;

void logger::print(std::string msg)
{
    {
        boost::mutex::scoped_lock instance_lock(logger::instance_mutex);
        if(!logger::instance) {
            logger::instance = new logger();
        }
    }

    instance->io_service.post(
        boost::bind(&logger::print_handler, instance, msg));
}

logger::logger() : work(io_service)
{
    thread = new boost::thread(
        boost::bind(
            &boost::asio::io_service::run, &io_service));     
}

logger::~logger()
{
    delete thread;
}

void logger::print_handler(std::string msg)
{
    std::cout << msg << std::endl;
}
